<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Wasfa extends CI_Controller {
 
    function __construct()
    {
        parent::__construct();
 
        /* Standard Libraries */
        $this->load->database();
        $this->load->helper('url');

        /* ------------------ */    
 
        $this->load->library('grocery_CRUD');
 
    }
 
    public function index()
    {
 
    }
 
    public function admin()
    {
        $crud = new grocery_CRUD();
 
        $crud->set_table('wasfa');
		$crud->required_fields('name','desc','image','SubCategory');
		$crud->fields('name','desc','image','date','SubCategory');
		$crud->change_field_type('date','invisible');
		$crud->set_relation_n_n('SubCategory', 'sub_wasfa', 'subcategory', 'wasfa_id', 'sub_id', 'name',null);
		$crud->set_field_upload('image','assets/uploads');
		$crud->callback_before_insert(array($this,'data_insert_callback'));
		$crud->callback_after_upload(array($this,'example_callback_after_upload'));
        $output = $crud->render();
 
        $this->_example_output($output);                
    }
	
	function data_insert_callback($post_array) {
		$post_array['date'] = date('Y-m-d H:i:s');
		return $post_array;
	}
	
	function example_callback_after_upload($uploader_response,$field_info, $files_to_upload)
	{
		$this->load->library('image_moo');
	 
		//Is only one file uploaded so it ok to use it with $uploader_response[0].
		$file_uploaded = $field_info->upload_path.'/'.$uploader_response[0]->name; 
		$file_uploaded_thumb = $field_info->upload_path.'/../thumbs/'.$uploader_response[0]->name; 
	 
		$this->image_moo->load($file_uploaded)->resize(200,100)->save($file_uploaded_thumb,true);
	 
		return true;
	}
 
    function _example_output($output = null)
    {
        $this->load->view('admin/admin.php',$output);    
    }    
}